package com.example.service.loader;

import com.example.persistence.PersistenceService;
import com.example.service.ServiceException;
import lombok.extern.slf4j.Slf4j;

import java.util.Optional;
import java.util.ServiceLoader;

/**
 * 查找并加载PersistenceService的实现
 *
 * @author 01
 * @date 2019-10-05
 **/
@Slf4j
public class PersistenceServiceLoader {

    private PersistenceServiceLoader() {
    }

    public static final PersistenceService persistenceService;

    static {
        final Optional<PersistenceService> first =
                ServiceLoader.load(PersistenceService.class).findFirst();
        if (first.isPresent()) {
            persistenceService = first.get();
        } else {
            throw new ServiceException("PersistenceService not found!!!");
        }
    }
}
